package com.HTMLParsers;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Entities.EscapeMode;
import org.jsoup.select.Elements;

import DBPack.DBStatement;

public class RouteParser {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub 
		try {
		for (int i=1;i<28;i++) {
		    String url = "http://mobile.istanbul.net.tr/istanbul-Ulasim/iett-Seferleri/1/"+i;
		    //Document doc = Jsoup.parse(input, "iso-8859-9", url);
		    Document doc = Jsoup.connect(url).timeout(6000).get();
		    doc.outputSettings().charset("UTF-8");
		    doc.outputSettings().escapeMode(EscapeMode.xhtml);

		    //Elements elements = doc.select("table.text a[href]");
		    Elements content = doc.select(".rightChannels li a");
		    
		    if (content !=null) {
		    for (int z=0;z<content.size();z++) {
		    	Elements elements = content.get(z).getElementsByTag("a");

		    for (int j=0;j<elements.size();j++) {
		    	  String title = elements.get(j).attr("title");
		    	  String routeCode=title.split("-")[0].trim();
		    	  String routeName=title.substring(title.indexOf("-")+1,title.length()).trim();
		    	  System.out.println(routeCode);
		    	  System.out.println(routeName);
		    	  insertRoute(routeCode,routeName);
		    	}
		    }
		    }
		    //String div=elements.toString();
		    //System.out.println(div);
		} 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void insertRoute(String routeCode,String routeName) {
		try{
			String query="INSERT INTO transportation.routes(route_code,route_name) VALUES (?, ?)";
			PreparedStatement ps=(PreparedStatement) DBStatement.getMainConnection().prepareStatement(query);
			ps.setString(1, routeCode);
			ps.setString(2, routeName);
			ps.executeUpdate();
		} catch(SQLException e) {
			System.err.println("SQLException: "+e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println(e.getMessage());
		}
		
	}
 
}
